/**
 * 
 */
package cn.ivicar.message.push.service;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.inject.Inject;

import cn.com.carsmart.ws.model.AppPush;
import cn.com.carsmart.ws.service.AppPushManager;

/**
 * @author liurunbin 2017年5月25日 cn.ivicar.message.push.service
 */
public class AppPushService {

	private static transient final Logger LOGGER = LoggerFactory.getLogger(AppPushService.class);

	@Inject
	private AppPushManager appPushManager;

	private static Cache<Long, AppPush> readCache = CacheBuilder.newBuilder().maximumSize(100)
			.expireAfterWrite(3, TimeUnit.HOURS).build();

	public AppPush get(final Long id) {
		AppPush push = null;
		try {
			push = readCache.get(id, new Callable<AppPush>() {

				@Override
				public AppPush call() throws Exception {
					LOGGER.info(String.format("加载推送应用[appPushId=%s]到localcache", id));
					return appPushManager.get(id);
				}

			});
		} catch (Exception e) {
			LOGGER.warn("获取推送应用失败", e);
		}
		return push;
	}
}
